﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common
{

    /// <summary>
    /// A class of which instances are savable and loadable to/from file as Xml
    /// </summary>
    public class SavableObject<TThis>
    {

        public static void Save(string filename, TThis obj)
        {
            var serializer = new XmlSerializer(typeof (TThis));
            serializer.Serialize(new XmlTextWriter(filename, Encoding.Unicode), obj);
        }

        public static TThis Load(string filename)
        {
            var serializer = new XmlSerializer(typeof(TThis));
            using (var stream = File.OpenText(filename))
            {
                return (TThis)serializer.Deserialize(stream);
            }
        }

    }
}
